package cn.comtom.ewbs.signature.common.config;


import javax.validation.ValidationException;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import cn.comtom.ewbs.signature.common.enums.BasicError;
import cn.comtom.ewbs.signature.common.exception.SignException;
import cn.comtom.ewbs.signature.common.vo.response.ApiResponse;
import cn.dev33.satoken.exception.DisableServiceException;
import cn.dev33.satoken.exception.NotBasicAuthException;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.exception.NotSafeException;
import lombok.extern.slf4j.Slf4j;

/**
 * 异常处理器
 *
 * @author liuhy
 */
@RestControllerAdvice
@Slf4j
public class ExceptionHandlerConfig {
	
    /**
     * 签名异常
     * @param e
     * @return
     */
    @ExceptionHandler(SignException.class)
    public ApiResponse handleSignException(SignException e) {
        return ApiResponse.error(BasicError.SIGN_ERROR.getCode(), e.getMsg());
    }
	
    /**
     * @param e 未登录异常
     * @return 响应数据
     */
    @ExceptionHandler(NotLoginException.class)
    public ApiResponse handleNotLoginException(NotLoginException e) {
        log.error(e.getMessage(), e);
        // 判断场景值，定制化异常信息
        if (e.getType().equals(NotLoginException.NOT_TOKEN)) {
        	return ApiResponse.error(BasicError.NOT_TOKEN_ERROR);
        }
        if (e.getType().equals(NotLoginException.INVALID_TOKEN)) {
        	return ApiResponse.error(BasicError.INVALID_TOKEN_ERROR);
        }
        if (e.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
        	return ApiResponse.error(BasicError.TOKEN_TIMEOUT_ERROR);
        }
        if (e.getType().equals(NotLoginException.BE_REPLACED)) {
        	return ApiResponse.error(BasicError.BE_REPLACED_ERROR);
        }
        if (e.getType().equals(NotLoginException.KICK_OUT)) {
        	return ApiResponse.error(BasicError.KICK_OUT_ERROR);
        } else {
        	return ApiResponse.error(BasicError.NOT_LOGIN_ERROR);
        }
    }
    
    /**
     * @param e 二级认证异常
     * @return 响应数据
     */
    @ExceptionHandler(NotSafeException.class)
    public ApiResponse handleNotSafeException(NotSafeException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.NOT_SAFE_AUTH);
    }
    
    /**
     * @param e http basic认证异常
     * @return 响应数据
     */
    @ExceptionHandler(NotBasicAuthException.class)
    public ApiResponse handleNotBasicAuthException(NotBasicAuthException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.NOT_BASIC_AUTH);
    }
    
    /**
     * @param e 无角色异常
     * @return 响应数据
     */
    @ExceptionHandler(NotRoleException.class)
    public ApiResponse handleNotRoleException(NotRoleException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.NOT_ROLE_ERROR);
    }
    
    /**
     * @param e 无权限异常
     * @return 响应数据
     */
    @ExceptionHandler(NotPermissionException.class)
    public ApiResponse handleNotPermissionException(NotPermissionException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.NOT_PERMISSION_ERROR);
    }
    
    /**
     * @param e 禁封异常
     * @return 响应数据
     */
    @ExceptionHandler(DisableServiceException.class)
    public ApiResponse handleDisableServiceException(DisableServiceException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.DISABLE_SERVICE_ERROR);
    }
    
    /**
     * @param e 参数校验异常
     * @return 响应数据
     */
    @ExceptionHandler(ValidationException.class)
    public ApiResponse handleValidationException(ValidationException e) {
        log.error(e.getMessage(), e);
        return ApiResponse.validError(e.getMessage());
    }
    
    /**
     * @param e 全局通用异常
     * @return 响应数据
     */
    @ExceptionHandler(Exception.class)
    public ApiResponse handleException(Exception e) {
        log.error(e.getMessage(), e);
        return ApiResponse.error(BasicError.OPT_FALID_VALUE);
    }
    
}
